package com.beermanager.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.beermanager.R;
import com.beermanager.components.actionbar.ActionBarActivity;
import com.beermanager.domain.Beer;
import com.beermanager.facebook.FacebookConnection;
import com.doffman.dragarea.DragArea;

public class BeerActivity extends ActionBarActivity {
    private LinearLayout mBeerListLayout;
    private DragArea mDragArea;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.beer_activity_layout);

        mBeerListLayout = (LinearLayout) findViewById(R.id.beers_list);
        mDragArea = (DragArea) findViewById(R.id.drag_area);

        ManDrinkingDraggable manDraggable = (ManDrinkingDraggable)findViewById(R.id.men_icon);
        manDraggable.setDragArea(mDragArea);
        manDraggable.setContext(this);
        
        
        BeerDraggable beerDraggable = new BeerDraggable(this, new Beer("Polar",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_light_blue)));
        addBeerDraggableToList(beerDraggable);

        beerDraggable = new BeerDraggable(this, new Beer("Original",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_orange)));
        addBeerDraggableToList(beerDraggable);

        beerDraggable = new BeerDraggable(this, new Beer("Heineken",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_green)));
        addBeerDraggableToList(beerDraggable);

        beerDraggable = new BeerDraggable(this, new Beer("Stella Artois",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_cyan)));
        addBeerDraggableToList(beerDraggable);

        beerDraggable = new BeerDraggable(this, new Beer("Quilmes",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_blue)));
        addBeerDraggableToList(beerDraggable);

        beerDraggable = new BeerDraggable(this, new Beer("Skol",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_yellow)));
        addBeerDraggableToList(beerDraggable);

        beerDraggable = new BeerDraggable(this, new Beer("Paulaner",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_red)));
        addBeerDraggableToList(beerDraggable);

        beerDraggable = new BeerDraggable(this, new Beer("Franziskaner",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_purple)));
        addBeerDraggableToList(beerDraggable);

        beerDraggable = new BeerDraggable(this, new Beer("Erdinger",
                BitmapFactory.decodeResource(getResources(), R.drawable.beer_rss_light_green)));
        addBeerDraggableToList(beerDraggable);
        
        
        FacebookConnection.setContext(this);

    }

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ManDrinkingDraggable.resetBeerCount();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_share:
			if(FacebookConnection.readyToPost()) {
				share();
			}
			else {
				Toast.makeText(getApplicationContext(), "Calma! Primeiro toma uma ceva, depois compartilha.", Toast.LENGTH_SHORT).show();
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void share() {
		final CharSequence[] items = {"Péssima", "Ruim", "Na média", "Boa", "Excelente"};

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Avalie sua experiência:");
		builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
		        FacebookConnection.postFacebookMessage(items[item].toString());
		        dialog.dismiss();
		    }
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.beer_activity_menu, menu);

        // Calling super after populating the menu is necessary here to ensure
        // that the
        // action bar helpers have a chance to handle this event.
        return super.onCreateOptionsMenu(menu);
    }

    private void addBeerDraggableToList(BeerDraggable beerDraggable){
        beerDraggable.setDragArea(mDragArea);

        registerForContextMenu(beerDraggable);

        mBeerListLayout.addView(beerDraggable);
        //persistenceManager.storeBar(beerDraggable.getBeer());
    }
}
